package com.base.app_core.View;

import android.content.Context;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.base.app_core.R;
import com.base.app_core.base.BaseView;

import static com.base.app_core.util.AppConstants.STATE_EMPTY;
import static com.base.app_core.util.AppConstants.STATE_ERROR;
import static com.base.app_core.util.AppConstants.STATE_LOADING;
import static com.base.app_core.util.AppConstants.STATE_SUCCESS;
import static com.base.app_core.util.AppConstants.STATE_UNKNOWN;


/**
 * Created by quantan.liu on 2017/3/2.
 */

public abstract class LoadingPage extends FrameLayout {
    private BaseView.ReTryFetch mretryFetch;

    public void setMretryFetch(BaseView.ReTryFetch mretryFetch) {
        this.mretryFetch = mretryFetch;
    }

    private View loadingView;                 // 加载中的界面
    private View errorView;                   // 错误界面
    private View emptyView;                   // 空界面
    public View contentView;                 // 加载成功的界面
    public int state = STATE_UNKNOWN;

    private Context mContext;

    public LoadingPage(Context context, boolean discomple) {
        this(context, null);
        state = !discomple ? STATE_SUCCESS : STATE_UNKNOWN;
        init();
    }

    public LoadingPage(Context context) {
        this(context, null);
        init();
    }

    public LoadingPage(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
        init();
    }

    public LoadingPage(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();//初始化4种界面
    }


    private void init() {
        this.setBackgroundColor(getResources().getColor(R.color.white));
        //把loadingView添加到frameLayout上
        if (loadingView == null) {
            loadingView = createLoadingView();
            this.addView(loadingView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        }
        //把emptyView添加到frameLayout上
        if (emptyView == null) {
            emptyView = createEmptyView();
            this.addView(emptyView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        }
        //把errorView添加到frameLayout上
        if (errorView == null) {
            errorView = createErrorView();
            this.addView(errorView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        }
        if (null == contentView) {
            contentView = createContentView();
        }
        showPage(state);//根据状态显示界面

    }


    private View createLoadingView() {
        loadingView = LayoutInflater.from(mContext).inflate(R.layout.basefragment_state_loading, null);
        return loadingView;
    }

    private TextView text_empty;

    private View createEmptyView() {
        emptyView = LayoutInflater.from(mContext).inflate(R.layout.basefragment_state_empty, null);
        text_empty = (TextView) emptyView.findViewById(R.id.text_empty);
        return emptyView;
    }

    private TextView text_error;

    private View createErrorView() {
        errorView = LayoutInflater.from(mContext).inflate(R.layout.basefragment_state_error, null);
        text_error = (TextView) errorView.findViewById(R.id.text_error);
        errorView.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View view) {
                loadData();
            }
        });
        return errorView;
    }

    private View createContentView() {
        if (contentView == null) {
            contentView = LayoutInflater.from(mContext).inflate(getLayoutId(), null);
            addView(contentView, LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
            initView();
        }
        return contentView;
    }

    public void showErrorPage(String content) {
        showPage(STATE_ERROR);
        if (null != text_error) {
            text_error.setText(content + ",请点击重试");
        }
    }

    public void showPage(int state1) {
        this.state = state1;
        if (loadingView != null) {
            if (state == STATE_UNKNOWN || state == STATE_LOADING) {
                loadingView.setVisibility(View.VISIBLE);
            } else {
                loadingView.setVisibility(View.GONE);
            }
        }
        if (emptyView != null) {
            if (null != emptyView)
                emptyView.setVisibility(state == STATE_EMPTY ? View.VISIBLE : View.GONE);
        }
        if (errorView != null) {
            if (null != errorView)
                errorView.setVisibility(state == STATE_ERROR ? View.VISIBLE : View.GONE);
        }
        if (state == STATE_SUCCESS) {
            if (null != contentView) contentView.setVisibility(View.VISIBLE);
        } else {
            if (null != contentView) contentView.setVisibility(View.GONE);
        }
    }

    /**
     * 3
     * 子类关于View的操作(如setAdapter)都必须在这里面，会因为页面状态不为成功，而binding还没创建就引用而导致空指针。
     */
    protected abstract void initView();

    /**
     * 1
     * 根据网络获取的数据返回状态，每一个子类的获取网络返回的都不一样，所以要交给子类去完成
     */
    protected abstract void loadData();


    protected abstract int getLayoutId();
}
